﻿
@{
    ViewBag.Title = "贷款类型管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table id="dg" class="easyui-datagrid" title="贷款类型管理" style="width:100%;height:auto"></table>
<script type="text/javascript">

    $(function () {
        $("#showimg").window("center");
        $(".nav-list li").removeClass("active");
        $("#admin_li").addClass("active");
        $('#dg').datagrid({
            rownumbers: true,
            checkOnSelect: false,
            url: '/type/Alltype',
            pagination: true,
            pageSize: 5,
            pageList: [5, 10, 15, 20],
            method: 'get',
            striped: true,
            singleSelect: true,
            toolbar: toolbar,
            selectOnCheck: false,
            fitColumns: true,
            loadMsg: "loading...",
            columns: [[
                {
                    field: 'ck',
                    title: "",
                    checkbox: true
                },
                {
                    field: 'actt',
                    title: '操作',
                    width: 60,
                    formatter: function (value, row, index) {
                        var _btn = '<button class="btn btn-inverse btn-xs" style="margin:3px;" onclick="editline(' + index + ',event)">编辑</button>' +
                            '<button class="btn btn-danger btn-xs" style="margin:3px;" onclick="deleteline(' + row.TypeId + ',event)">删除</button>'
                        return _btn;
                    }
                },
                { field: 'TypeId', title: '贷款类型ID', width: 30 },
                { field: 'Typename', title: '贷款项目名称', width: 40 },
                { field: 'Sum', title: '金额', width: 30 },
                { field: 'LastModifyDate', title: '最后更新时间', width: 40 },
                { field: 'LastModifyUser', title: '最后更新人', width: 40 }
            ]],
            onHeaderContextMenu: function (e, field) {
                e.preventDefault();

                if (!cmenu) {
                    createColumnMenu();
                }
                cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });
    });


    var toolbar = [{
        text: '添加',
        iconCls: 'icon-add',
        handler: function (elem) {

            $("#form_add").form("clear");
            $("#form_add").attr("action", "/type/Add");
            $("#win_add").window("open");
        }
    }, {
        text: '批量删除',
        iconCls: 'icon-remove',
        handler: function (elem) {
            var rows = $("#dg").datagrid("getChecked");
            if (confirm("确认删除选中条目？")) {
                var IDs = [];
                var i = 0;
                while (i < rows.length) {
                    IDs.push(rows[i++].G_ID);
                }

                var param = {};
                param.IDs = IDs;

                $.ajax({
                    type: 'get',
                    data: param,
                    url: '/Home/BatDel',
                    traditional: true,
                    success: function (data) {
                        if (data > 0)
                            $.messager.alert('提示', '已删除' + data + "条数据.", 'info');
                        else
                            $.messager.alert('错误', '删除失败！', 'error');
                        $("#dg").datagrid("reload");
                    }
                });
            }
        }
    }];
    function editline(obj, event) {
        var e = event || window.event;
        e.stopPropagation();

        var row = $("#dg").datagrid("getRows")[obj];

        $("#win_add").attr("title", "编辑");
        $("#form_add").form("clear");
        $("#form_add").attr("action", "/type/Edit/" +row.TypeId );
        $("#form_add").form("load", row);
        $("#win_add").window("open");
    }

    function deleteline(TypeId, event) {
        var e = event || window.event;
        e.stopPropagation();
        if (confirm("确认删除该条目？")) {
            $.ajax({
                type: 'post',
                url: '/type/Delete/' + TypeId,
                success: function (data) {
                    if (data == 0) {
                        $.messager.alert('提示', '删除成功', 'info');
                        $("#dg").datagrid("reload");
                    }

                    else {
                        $.messager.alert('警告', '失败！', 'warning');
                        $("#dg").datagrid("reload");
                    }
                       
                    
                }
            });
        }
    }

    function submit(id) {

        $(id).form('submit', {
            success: function (data) {
                if (data == 0) {
                    alert("成功。");
                    $("#win_add").window("close");
                    $("#dg").datagrid("reload");
                }
                else {
                    alert("失败。");
                    $("#win_add").window("close");
                    $("#dg").datagrid("reload");
                }
                Page();
                //Search($("#dg").datagrid("getPager").pagination("options").pageNumber);
                //$("#win_add").window("close");
                //$("#dg").datagrid("reload");
            }
        });
    }
    function clearForm(obj) {
        $(obj).form("clear");
    }
    //分页函数
    function Page(){
        var val = {};
        val.pageNumber = $("#dg").datagrid("getPager").pagination("options").pageNumber;
        val.pageSize = $("#dg").datagrid("getPager").pagination("options").pageSize;
        $.ajax({
            type: 'get',
            url: '/type/ALLtype',
            async: true,
            datetype: 'json',
            data: val,
            success: function (date) {
                $('#dg').datagrid('loadData', date);
                $("#dg").datagrid("getPager").pagination({
                    onSelectPage: function (pageNumber, pageSize) {
                        Page();
                    },
                    onRefresh: function (pageNumber, pageSize) {
                        page();
                    }
                })
            }
        });
    }
</script>

<div id="win_add" class="easyui-window" title="添加" style="width:350px;height:280px;display:none;" data-options="iconCls:'icon-edit',shadow:false,collapsible:false,minimizable:false,closed:true,modal:true">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center'" style="text-align:center;">
            <div class="space-20"></div>
            <form class="window_form" id="form_add" method="post">
                
                <div>
                    <label>贷款项目名称:</label><input class="easyui-textbox" singleSelect="true" name="Typename" data-options="required:true,width:200" />
                </div>
                <div>
                    <label>金额&nbsp;&nbsp;&nbsp;&nbsp;:</label><input class="easyui-textbox" name="Sum" data-options="required:true,width:200" />
                </div>
                <div class="space-10"></div>
                <div style="text-align:center;padding:5px 0">
                    <a id="clearForm" href="javascript:void(0)" class="easyui-linkbutton" onclick="submit('#form_add')" style="width:80px;height:30px">提交</a>
                    <a id="clearForm" href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm('#form_add')" style="width:80px;height:30px">清空</a>
                </div>
            </form>
        </div>
    </div>
</div>
